package com.tmzn.transdata.util;

import java.util.Arrays;

public class MsgUtil {

    public static void main(String[] args) {
        //0110 0100 0000
        MsgUtil messageUtil = new MsgUtil();
        Integer arr [] ={139,244,2,234};
        System.out.println(messageUtil.cmdToDec(arr));
//        System.out.println(Arrays.toString(messageUtil.decToCmd(messageUtil.cmdToDec(arr))));
        System.out.println(Arrays.toString(decToCmd(10)));
    }
    /**
     * 将命令字符串转成十进制
     * @param cmds
     * @return
     */
    public static Long cmdToDec(Integer [] cmds){
        if(cmds == null){
            return -1L;
        }
        int len = cmds.length;
        if(len == 1){
            return cmds[0].longValue();
        }else if(len == 2){
            int first = cmds[0];
            int second = cmds[1];
            String firstStr =addZero(Integer.toString(first, 2));
            String secondStr = addZero(Integer.toString(second, 2));
            String allStr = secondStr+""+firstStr;
            Long i = Long.parseLong(allStr,2);
            return i;
        }else if(len == 4){
            int first = cmds[0];
            int second = cmds[1];
            int third = cmds[2];
            int forth = cmds[3];
            String firstStr = addZero(Integer.toString(first, 2));
            String secondStr = addZero(Integer.toString(second, 2));
            String thirdStr =  addZero(Integer.toString(third, 2));
            String forthStr =  addZero(Integer.toString(forth, 2));
            String allStr = forthStr+""+thirdStr+""+secondStr+""+firstStr;
            Long i = Long.parseLong(allStr,2);
            return i;
        }
        return 0L;
    }

    /**
     * 二进制补0
     * @param value
     * @return
     */
    public static String addZero(String value){
        int len = value.length();
        if(len<8){
            for (int i = 0; i <8-len ; i++) {
                value = "0"+value;
            }
        }
        return value;
    }
    /**
     * 将十进制值转成cmd格式
     * @param value
     * @return
     */
    public static Integer [] decToCmd(int value){
        Integer cmds[] = null;
        if(value<=255){ //两位
            cmds = new Integer[1];
            cmds[0] = value;
        }else if(value <=65535){
            cmds = new Integer[2];
            String valueStr = Integer.toBinaryString(value);
            int len = valueStr.length();
            String lastEight = valueStr.substring(len-8,len);
            String firstEight = valueStr.substring(0,len-8);
            cmds[0] = Integer.parseInt(lastEight,2);
            cmds[1] =  Integer.parseInt(firstEight,2);

        }else{
            cmds = new Integer[4];
            String valueStr = Integer.toBinaryString(value);
            int len = valueStr.length();
            String lastEight = valueStr.substring(len-8,len);
            String firstEight = valueStr.substring(len-16,len-8);
            String secondEight = valueStr.substring(len-24,len-16);
            String thirdEight = valueStr.substring(0,len-24);;
            cmds[0] = Integer.parseInt(lastEight,2);
            cmds[1] =  Integer.parseInt(firstEight,2);
            cmds[2] =  Integer.parseInt(secondEight,2);
            cmds[3] =  Integer.parseInt(thirdEight,2);
        }
        return cmds;
    }
}
